package pwd.allen.converter;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils;
import com.alibaba.excel.write.executor.AbstractExcelWriteExecutor;
import pwd.allen.DicItem;

import java.util.HashMap;
import java.util.Map;

/**
 * 自定义数据字典映射
 *
 * 使用操作：实体类中要转换的字段的@ExcelProperty注解的convert属性指定为本类
 * 缺点：不支持默认值
 */
public class DicConverter implements Converter<Object> {

    private Map<String, Map<String, String>> map_dic = new HashMap<>();

    {
        // 这里实际项目中可以改成从数据库中读取，这里先写死
        map_dic.put("str数据字典", MapUtil.<String, String>builder().put("str1", "字符串1").put("str2", "字符串2").build());
        map_dic.put("int数据字典", MapUtil.<String, String>builder().put("1", "数字1").put("2", "数字2").build());
    }

    @Override
    public Class supportJavaTypeKey() {
        return Object.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
                                    GlobalConfiguration globalConfiguration) {
        return cellData.getStringValue();
    }

    /**
     * 每个单元格会convert，convert时会调用这里，convert之后会调用CellWriteHandler
     * @see AbstractExcelWriteExecutor#convert(com.alibaba.excel.write.metadata.holder.WriteHolder, java.lang.Class, org.apache.poi.ss.usermodel.Cell, java.lang.Object, com.alibaba.excel.metadata.property.ExcelContentProperty)
     * @param value
     * @param contentProperty
     * @param globalConfiguration
     * @return
     */
    @Override
    public CellData convertToExcelData(Object value, ExcelContentProperty contentProperty,
                                       GlobalConfiguration globalConfiguration) {
        DicItem annotation = contentProperty.getField().getAnnotation(DicItem.class);
        if (annotation != null) {
            if (value == null) {
                // 不会执行到这里，因为value为null的话easyexcel是直接返回new CellData(CellDataTypeEnum.EMPTY)的
                // 要实现没有值时赋予默认值，需要用CellWriteHandler，
                return StrUtil.isNotEmpty(annotation.def()) ? new CellData(annotation.def()) : null;
            }
            if (StrUtil.isNotEmpty(annotation.json())) {
                JSONObject jsonObject = JSONUtil.parseObj(annotation.json());
                return new CellData(jsonObject.getStr(value.toString(), value.toString()));
            }
            if (annotation != null && map_dic != null) {
                Map<String, String> dic = map_dic.get(annotation.value());
                if (dic != null && dic.containsKey(value)) {
                    return new CellData(dic.get(value));
                }
            }
        }
        return new CellData(value != null? value.toString() : null);
    }

}
